import { FMVCS } from "../../fgui/fmvc/FMVCS";
import LoginData from "./LoginData";
import Manager from "../../manager/Manager";
import { ConstDefine } from "../common/ConstDefine";
import LoginView from "./LoginView";
import { GlobalEvent } from "../global/Global";
import { UserLogin } from "../../gamenet/protobufs/userprotocol/protocol_001";
import PlayerCtrl from "../player/PlayerCtrl";
import { ProtocolPool } from "../../gamenet/protobufs/base/BaseProtocol";
import { NetDefine } from "../../gamenet/protobufs/NetDefine";
import { Platform } from "../../platform/Platform";
import Utils from "../../utils/Utils";
import LocalStore from "../../utils/LocalStore";

export default class LoginCtrl extends FMVCS.FCtrl {
    /** 数据实体 */
    static data: LoginData = null;
    static instance: LoginCtrl = null;

    loginCode: string = null;
    localGuestKeyName: string = `guestKey`;

    constructor() {
        super();
        LoginCtrl.instance = this;
        LoginCtrl.data = new LoginData();
    }

    /** 事件监听注册 */
    rigisterEventListener() {
        this.on(GlobalEvent.UIListenID.UI_LoginView_Open, this.openView, this);
        this.rigisterProtocol(UserLogin, `onPlayerLoginRecv`);
    }

    openView() {
        Manager.fui.open(ConstDefine.UI.LoginView, LoginView);
    }

    closeView() {
        Manager.fui.close(ConstDefine.UI.LoginView);
    }

    userLogin(loginType: NetDefine.eLoginType) {
        let proto = ProtocolPool.instance.getProtocol(UserLogin);
        let platform = Platform.instance;
        let send = (code: string) => {
            proto.key = code;
            proto.loginType = loginType;
            proto.encodeAndSend();
        }
        switch (loginType) {
            case NetDefine.eLoginType.Guest: {
                let code = LocalStore.getItem(this.localGuestKeyName);
                if (!code) {
                    code = Utils.randomString(32);
                }
                this.loginCode = code;
                console.log(`[LoginCtrl] --> code`, this.loginCode);
                send(code);
                break;
            }
            case NetDefine.eLoginType.WeChat: {
                platform.silentLogin().then((code: string) => {
                    console.log(`[LoginCtrl] --> code`, code);
                    this.loginCode = code;
                    send(this.loginCode);
                });
                break;
            }
        }
    }

    onPlayerLoginRecv(protocol: UserLogin) {
        let vo = PlayerCtrl.data.vo;
        vo.nickName = protocol.nick;
        vo.icon = protocol.icon;
        vo.face = protocol.uface;
        vo.sex = protocol.usex;
        vo.vip = protocol.uvip;
        this.emit(GlobalEvent.GameEventID.UserLoginSuc);
        if (protocol.loginType == NetDefine.eLoginType.Guest) {
            LocalStore.setItem(this.localGuestKeyName, protocol.key);
        }
        this.onLoginSuccess();
    }

    onLoginSuccess() {
        this.emit(GlobalEvent.UIListenID.UI_HomeView_Open);
        this.closeView();
    }
}
